package com.jackstraw.jack_base.datasource.jpa.service.impl;

import com.jackstraw.jack_base.datasource.common.aspect.annotation.DataSourceNameCut;
import com.jackstraw.jack_base.datasource.common.entity.enums.DataSourceTypeEnum;
import com.jackstraw.jack_base.datasource.database.parten.base.entity.page.PageData;
import com.jackstraw.jack_base.datasource.jpa.base.service.impl.JpaServiceImpl;
import com.jackstraw.jack_base.datasource.jpa.dao.database.UserDao;
import com.jackstraw.jack_base.datasource.jpa.entity.bo.UserBo;
import com.jackstraw.jack_base.datasource.jpa.entity.po.UserPo;
import com.jackstraw.jack_base.datasource.jpa.service.UserService;
import com.jackstraw.jack_base.util.entity.DefaultConstants;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Slf4j
@DataSourceNameCut(dataSourceTypeEnum = DataSourceTypeEnum.database, dataSourceName = "dataBaseName1")
public class UserServiceImpl extends JpaServiceImpl<UserDao, UserPo, UserBo, String> implements UserService {

    @Override
    public void aaa() {

        UserBo userBo = new UserBo();
        userBo.setEmail("999");
        userBo.setLastName("jack");
        this.insert(userBo);

        log.info("{}", userBo);


        List<UserBo> userBos = new ArrayList<>();

        for (int i = 0; i < 10; i++) {
            UserBo userBo1 = new UserBo();
            userBo1.setEmail("aaa" + i);
            userBo1.setLastName("aaa" + i);
            userBos.add(userBo1);
        }

        List<UserBo> userBos1 = this.insertBatch(userBos);

        Map<String, Object> params = new HashMap<>();

        Map<String, String> sortMap = new HashMap<>();

        sortMap.put("lastName", "desc");
        sortMap.put("email", "asc");

        params.put(DefaultConstants.PAGE_NUM, 1);
        params.put(DefaultConstants.PAGE_SIZE, 5);

        params.put(DefaultConstants.ORDER, sortMap);

        params.put(DefaultConstants.ORDER_FIELD, "lastName,email");

        log.info("{}", userBos1);

        List<UserBo> list = this.list(params);

        PageData<UserBo> page = this.page(params);


        log.info("{}", list);


        params.put("id", userBo.getId());


        List<UserBo> list1 = this.list(params);

        log.info("{}", list1);

        UserBo userBo1 = this.get(userBo.getId());


        List<String> collect = Arrays.asList(userBo1.getId());
        List<UserBo> userBos2 = this.get(collect);




        log.info("{}", userBos2);


        userBo1.setLastName("xiugai");

        this.update(userBo1);


        UserBo userBo3= this.get(userBo.getId());

        log.info("{}", userBo3);


        userBos2.stream().forEach(userBo2 -> userBo2.setLastName(userBo2.getLastName().concat("piliangxiugfai")));

        this.updateBatch(userBos2);


        List<UserBo> userBos3 = this.get(userBos2.stream().map(UserBo::getId).collect(Collectors.toList()));



        log.info("{}", userBos3);


        this.delete(userBo3.keyValue());
        UserBo userBo2 = this.get(userBo.getId());

        log.info("{}", userBo2);


        List<UserBo> list2 = this.list(null);


        log.info("{}", list2);


        this.deleteBatch(boToKeyList(list2));
        List<UserBo> list3 = this.list(null);

        log.info("{}", list3);


    }
}
